﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using CuaHangBanhNgotBeoBeo.Game.View;
using CuaHangBanhNgotBeoBeo.Game.ViewModel;

namespace CuaHangBanhNgotBeoBeo {
    public partial class Main : PhoneApplicationPage {
        public Main() {
            InitializeComponent();
            this.Loaded += Main_Loaded;
        }

        void Main_Loaded(object sender, RoutedEventArgs e) {
            App.MainViewModel.LoadData();

            this.DataContext = App.MainViewModel;

            //Render holes
            RenderHoles();

            ItemViewModel vm = new ItemViewModel();

            vm.Rule = ItemViewModel.RULE_0;
            this.itemView.DataContext = vm;

            ItemViewModel vm2 = new ItemViewModel();

            vm2.Rule = ItemViewModel.RULE_1;
            this.itemView2.DataContext = vm2;

            ItemViewModel vm3 = new ItemViewModel();

            vm3.Rule = ItemViewModel.RULE_2;
            this.itemView3.DataContext = vm3;
        }

        private List<HoleView> _holeViews = new List<HoleView>();

        private void RenderHoles() {

            foreach (var hole in App.MainViewModel.HoleViewModels) {

                // New a HoleView and add to canvasMain
                var holeView = new HoleView();
                holeView.Width = holeView.Height = MainViewModel.SIZE_HOLE_WH;
                holeView.SetValue(Canvas.TopProperty, hole.TopPosition);
                holeView.SetValue(Canvas.LeftProperty, hole.LeftPosition);
                holeView.SetValue(Canvas.ZIndexProperty, 0);
                canvasMain.Children.Add(holeView);

                holeView.DataContext = hole;
                holeView.Name = "hole(" + hole.IPos.ToString() + "," + hole.JPos.ToString() + ")";

                _holeViews.Add(holeView);
            }
        }

        
    }
}